<?php

namespace app\admin\controller;

use think\App;
use tp5er\Backup;

class Database extends Base
{
    protected $backup;

    public function __construct(App $app = null)
    {
        parent::__construct($app);
        $this->backup = new Backup();
    }

    /**
     * 获取数据库中表的列表信息
     * @return \think\response\View
     */
    public function index()
    {
        $lists = $this->backup->dataList();

        return view('database/index', compact('lists'));
    }


    /**
     * 修复表
     * @return \think\response\Json
     * @throws \Exception
     */
    public function repair()
    {
        if ($this->request->isPost()) {
            $table = input("post.tablename");
            if (!empty($table)) {
                $result = $this->backup->repair($table);
                if ($result[0]['Msg_text'] == "OK") {
                    return writeJson(1, '修复成功');
                }

                return writeJson(0, $result[0]['Msg_text']);
            }
        } else {
            return writeJson(0, '请求异常');
        }
    }

    /**
     * 优化表
     * @return \think\response\Json
     * @throws \Exception
     */
    public function optimize()
    {
        if ($this->request->isPost()) {
            $table = input("post.tablename");
            if (!empty($table)) {
                $result = $this->backup->optimize($table);
                if ($result[0]['Msg_text'] == "OK") {
                    return writeJson(1, '优化成功');
                }

                return writeJson(0, $result[0]['Msg_text']);
            }
        } else {
            return writeJson(0, '请求异常');
        }
    }

    /**
     * 获取备份列表
     * @return \think\response\View
     * @throws \Exception
     */
    public function backuplst()
    {
        $table  = input('t');
        $config = [
            'path' => './Data/'.$table.DIRECTORY_SEPARATOR,
        ];
        $data   = [];
        $bk     = new Backup($config);
        $list   = $bk->fileList();
        foreach ($list as $v) {
            $res      = $bk->getFile('time', $v['time']);
            $filename = "未知";
            if (isset($res[0])) {
                $filename = basename($res[0]);
            }
            $v['filename'] = $filename;
            $data[]        = $v;
        }
        $this->assign('table', $table);
        $this->assign('list', $data);

        return view();
    }

    /**
     * 单表备份
     * @return \think\response\Json
     * @throws \Exception
     */
    public function dbBackup()
    {
        $table  = input("t");
        $file   = ['name' => date('Ymd-His'), 'part' => 1];
        $config = [
            'path'     => './Data/'.$table.DIRECTORY_SEPARATOR,
            'compress' => 1,
        ];
        $bk     = new Backup($config);
        $start  = $bk->setFile($file)->backup($table, 0);

        if ($start == 0) {
            return writeJson(1, '备份成功');
        }

        return writeJson(0, '备份失败');
    }

    /**
     * 还原表数据 => 数据量大的，提示效果会慢一点，不是报错
     * @return \think\response\Json
     * @throws \Exception
     */
    public function recycle()
    {
        $table  = input("post.t");
        $time   = input("post.time");
        $config = [
            'path'     => './Data/'.$table.DIRECTORY_SEPARATOR,
            'compress' => 1,
        ];
        $bk     = new Backup($config);
        $start  = 0;
        $start  = $bk->import($start, $time);
        if ($start == 0) {
            return writeJson(1, '导入成功');
        }

        return writeJson(0, '导入失败');
    }

    /**
     * 删除单个备份
     * @return \think\response\Json
     * @throws \Exception
     */
    public function del()
    {
        $table  = input("post.t");
        $time   = input("post.time");
        $config = [
            'path'     => './Data/'.$table.DIRECTORY_SEPARATOR,
            'compress' => 1,
        ];
        $bk     = new Backup($config);
        $result = $bk->delFile($time);
        if ($result == $time) {
            return writeJson(1, '删除成功');
        }

        return writeJson(0, '删除失败');
    }
}
